var gradeSubjectMap;

var getSubjectList = function(){
    $.ajax({
        type: "POST",
        url: "../api/home/getGradeAndSubject",
        contentType: "application/x-www-form-urlencoded",
        success: function (r) {
            if (r.code === 0) {
                gradeSubjectMap = r.gradeSubjectMap;
                getUserInfo();
                fillGradeData();
            } else {
                $.toast(r.msg, "text");
            }
        }
    });
}

var fillGradeData = function(){
    /* <![CDATA[ */
    var html = '';
    for(var key in gradeSubjectMap){
        html +='<option value="'+key+'">'+gradeSubjectMap[key][0].gradeName+'</option>'
    }
    /* ]]> */
    $("#showGrade").append(html);
}

var showSelectIdent = function (){
    $.modal({
        title: "身份",
        text: "请选择您的身份，我们之后会根据您选择的身份给你推送对应的内容，请慎重选择",
        buttons: [
            { text: "学生", onClick: function(){setIdent(0)} },
            { text: "家长", onClick: function(){setIdent(1)} },
            { text: "老师", onClick: function(){setIdent(2)} },
        ]
    });
}

var setIdent = function (ident) {
    $.ajax({
        type: "POST",
        url: "../api/default/saveUserIdentity",
        data:{
            identity : ident
        },
        contentType: "application/x-www-form-urlencoded",
        success: function (r) {
            if (r.code === 0) {
                $.toast("设置身份成功", "text");
            } else {
                $.toast(r.msg, "text");
            }
        }
    });
}

var getUserInfo = function(){
    $.ajax({
        type: "POST",
        url: "../api/home/getUserInfo",
        contentType: "application/x-www-form-urlencoded",
        success: function (r) {
            if (r.code === 0) {
                $("#showGrade").val(r.gradeId);
                //展示数据，因为ajax是异步的，可能会有问题
                var subjectList = gradeSubjectMap[r.gradeId];
                if(gradeSubjectMap[r.gradeId] == null || gradeSubjectMap[r.gradeId].length == 0){
                    for(var key in gradeSubjectMap){
                        $("#showGrade").val(key);

                    }
                }
                setSubjectList(subjectList);
                if(!r.identity){
                    showSelectIdent();
                }
            } else {
                $.toast(r.msg, "text");
            }
        }
    });
}

$(document).on('change','#showGrade',function () {
    //重新设置科目信息
    var subjectList = gradeSubjectMap[$("#showGrade").val()];
    setSubjectList(subjectList);
})

var sequence = function (a,b){
    return a.subjectOrder - b.subjectOrder;
}

var setSubjectList = function(subjectList){
    subjectList = subjectList.sort(sequence);
    var html = '';
    /* <![CDATA[ */
    subjectList.forEach(function(value,index,array){
        var gradeId= value.gradeId;
        var subjectId = value.subjectId;
        html += '<a href="./video-list.html?gradeId='+gradeId+'&subjectId='+subjectId+'" class="weui-grid js_grid">';
        html += '<div class="weui-grid__icon">';
        html += '<img src="'+value.subjectImg+'" alt="" />';
        html += '</div>';
        html += '<p class="weui-grid__label">';
        html +=  value.subjectName;
        html += '</p>';
        html += '</a>';
    });
    /* ]]> */
    $("#showSubjectList").html(html);
}
